/**
 * 
 */
package com.geekteam.manga.ui;

import java.util.Stack;

import android.os.Bundle;
import android.support.v4.app.Fragment;

import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.geekteam.manga.R;

/**
 * @author MtRol
 *
 */
public abstract class BaseActivity extends SherlockFragmentActivity{

	private Fragment mContent;
	private Stack<Fragment> mStackFragments;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		if (getSupportActionBar() != null) {
			getSupportActionBar().setLogo(
					getResources().getDrawable(
							R.drawable.ic_launcher));
			getSupportActionBar().setDisplayHomeAsUpEnabled(false);
		}
		
		mStackFragments = new Stack<Fragment>();

		if (savedInstanceState != null)
			mContent = getSupportFragmentManager().getFragment(
					savedInstanceState, "mContent");
		if (mContent == null) {
			mContent = createFragment();
			mStackFragments.add(mContent);
		}
		
		getSupportFragmentManager().beginTransaction()
		.replace(android.R.id.content, mContent).commit();
	}
	
	@Override
	protected void onResume() {
		super.onResume();
		getSupportActionBar().setTitle(getFragmentTitle());
	}
	
	@Override
	protected void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		getSupportFragmentManager().putFragment(outState, "mContent", mContent);
	}
	
	@Override
	protected void onRestoreInstanceState(Bundle savedInstanceState) {
		super.onRestoreInstanceState(savedInstanceState);
		mContent = getSupportFragmentManager().getFragment(
				savedInstanceState, "mContent");
	}
	
	protected abstract String getFragmentTitle();
	protected abstract Fragment createFragment();
	
}
